//
//  UIImage+SEGTheme.m
//  SEGTheme
//
//  Created by Joeyoung on 2020/3/21.
//  Copyright © 2020 Joeyoung. All rights reserved.
//

#import "UIImage+SEGTheme.h"
#import <objc/runtime.h>

static NSString *_darkName = nil;
@implementation UIImage (SEGTheme)

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class aClass = self.class;
        SEL originalSel = @selector(imageWithContentsOfFile:);
        SEL swizzleSel = @selector(seg_imageWithContentsOfFile:);
        Method originalMethod = class_getClassMethod(aClass, originalSel);
        Method swizzleMethod = class_getClassMethod(aClass, swizzleSel);
        Class metaClass = objc_getMetaClass(NSStringFromClass(aClass).UTF8String);
        BOOL didAddMethod = class_addMethod(metaClass,
                                            originalSel,
                                            method_getImplementation(swizzleMethod),
                                            method_getTypeEncoding(swizzleMethod));
        if (didAddMethod) {
            class_replaceMethod(metaClass,
                                swizzleSel,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzleMethod);
        }
    });
}
/// hook
+ (UIImage *)seg_imageWithContentsOfFile:(NSString *)path {
    UIImage *image = [UIImage seg_imageWithContentsOfFile:path];
    image.anyNamePath = path;
    if (!path || path.length == 0) return image;

    if (@available(iOS 13.0, *)) {
        NSString *picEnd = @"_dark";
        NSString *extension = [path pathExtension];
        if (extension.length > 0) { // 有拓展名
            if (![[path stringByDeletingPathExtension] hasSuffix:picEnd]) { // 不以 "_dark" 结尾
                image.anyNamePath = path;
                image.darkNamePath = [[[path stringByDeletingPathExtension] stringByAppendingString:picEnd] stringByAppendingPathExtension:extension];
            } else {
                image.darkNamePath = path;
                image.anyNamePath = [[[path stringByDeletingPathExtension] stringByReplacingOccurrencesOfString:picEnd withString:@""] stringByAppendingPathExtension:extension];
            }
        } else { // 无拓展名
            if (![path hasSuffix:picEnd]) { // 不以 "_dark" 结尾
                image.anyNamePath = path;
                image.darkNamePath = [path stringByAppendingString:picEnd];
            } else {
                image.darkNamePath = path;
                image.anyNamePath = [path stringByReplacingOccurrencesOfString:picEnd withString:@""];
            }
        }
        
        // 暗黑模式 返回暗黑图片
        NSInteger style = [UIApplication sharedApplication].keyWindow.traitCollection.userInterfaceStyle;
        if (style == UIUserInterfaceStyleDark) {
            UIImage *darkImage = [UIImage seg_imageWithContentsOfFile:image.darkNamePath];
            if (darkImage) {
                darkImage.anyNamePath = image.anyNamePath;
                darkImage.darkNamePath = image.darkNamePath;
                return darkImage;
            }
        }
    }
    
    return image;
}
#endif

#pragma mark - Associated object
- (void)setAnyNamePath:(NSString *)anyNamePath {
    objc_setAssociatedObject(self, @selector(anyNamePath), anyNamePath, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (NSString *)anyNamePath {
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setDarkNamePath:(NSString *)darkNamePath {
    objc_setAssociatedObject(self, @selector(darkNamePath), darkNamePath, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (NSString *)darkNamePath {
    return objc_getAssociatedObject(self, _cmd);
}

@end
